package com.zzzzzz.oms.legs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.WebUtils;
import com.zzzzzz.core.datagrid.BaseData;
import com.zzzzzz.core.datagrid.BaseQueryForm;
import com.zzzzzz.utils.Arith;
import com.zzzzzz.utils.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zzzzzz.oms.GCS;
import com.zzzzzz.oms.orderHead.OrderHead;
import com.zzzzzz.oms.orderHead.OrderHeadDao;
import com.zzzzzz.oms.receipt.ReceiptDao;
import com.zzzzzz.oms.tempShipment.TempShipmentDao;
import com.google.common.collect.Lists;
import com.zzzzzz.plugins.shiro.ShiroUtils;
import com.zzzzzz.plugins.poi.ExportExcel;
import com.zzzzzz.plugins.poi.ImportExcel;

/**
 * @author hing
 * @version 1.0.0
 */
@Controller
public class LegsWeb {

	@Resource
	public LegsService legsService;
	@Resource
	public LegsDao legsDao;
	@Resource
	public OrderHeadDao orderHeadDao;
	@Resource
	public TempShipmentDao tempShipmentDao;
	@Resource
	public ReceiptDao orderMonitorDao;
	@RequestMapping(value = "/legs/list", method = RequestMethod.GET)
	public String list() {
		return "oms/legs";
	}
	
	@RequestMapping(value = "/legs/save", method = RequestMethod.POST)
	@ResponseBody
	public BaseData save(@RequestBody @Valid Legs legs, BindingResult bindingResult) throws Exception {
		Legs leg=legsDao.findById(legs.getId());
		if (bindingResult.hasErrors()||Arith.sub(leg.getQuantity(), legs.getQuantity())<0
				||Arith.sub(leg.getWeight(), legs.getWeight())<0||Arith.sub(leg.getVolume(), legs.getVolume())<0||Arith.sub(leg.getPalletsum(), legs.getPalletsum())<0) {
			return new BaseData(GCS.ERRCD_PARAM);
		}
		int i=legsService.save(legs,leg, ShiroUtils.findUser());
		if(i==-1){
			return new BaseData("-1");
		}
		return new BaseData("0");
	}
	
	@RequestMapping(value = "/legs/updTempshipmenByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updTempshipmenByIds(@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "tempshipmentId", required = true)Long tempshipmentId) throws Exception {
		legsDao.updTempshipmenByIds(tempshipmentId, ids, ShiroUtils.findUser());
		return new BaseData("0");
	}	
	
	@RequestMapping(value = "/legs/updStByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updStByIds(@RequestBody List<Legs> legs,@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "st", required = true)String st) throws Exception {
		legsService.updMustDtByOrderId(legs);
		legsDao.updStByIds(ids, st,ShiroUtils.findUser());
		return new BaseData("0");
	}	
	
	
	@RequestMapping(value = "/legs/updStByWaveIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updStByWaveIds(@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "waveId", required = true)Long waveId) throws Exception {
		legsDao.updStByWaveIds(ids, waveId, ShiroUtils.findUser());
		return new BaseData("0");
	}
	
	@RequestMapping(value = "/legs/updByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updByIds(@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "orderId", required = true) List<Long> orderId,@RequestParam(value = "vocationType", required = true) String vocationType,@RequestParam(value = "shipmentId", required = true) Long shipmentId) throws Exception {
		legsService.updByIds (ids, vocationType, shipmentId, ShiroUtils.findUser());
		orderHeadDao.updStByIds(orderId, "ARRIVED",ShiroUtils.findUser());
		return new BaseData("0");
	}	
	@RequestMapping(value = "/legs/updByIds1", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updByIds1(@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "shipmentId", required = true) Long shipmentId,@RequestParam(value = "constractorId", required = true) Long constractorId,@RequestParam(value = "shipment_method", required = true) String shipment_method,@RequestParam(value = "vocationType", required = true) String vocationType) throws Exception {
		legsService.updByIds1(ids, shipmentId, constractorId, shipment_method, vocationType, ShiroUtils.findUser());
		return new BaseData("0");
	}
	@RequestMapping(value = "/legs/updByTempShipmentId", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updByTempShipmentId(@RequestParam(value = "tempShipmentId", required = true) Long tempShipmentId,@RequestParam(value = "constractorId", required = true)Long constractorId,@RequestParam(value = "shipment_method", required = true)String shipment_method,@RequestParam(value = "st", required = true)String st) throws Exception {
		legsDao.updByTempShipmentId(tempShipmentId, constractorId,shipment_method,st,ShiroUtils.findUser());
		return new BaseData("0");
	}	
	
	@RequestMapping(value = "/legs/updShipmentId", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updShipmentId(@RequestParam(value = "tempShipmentId", required = true) Long tempShipmentId,@RequestParam(value = "shipmentId", required = true)Long shipmentId) throws Exception {
		legsDao.updShipmentId(tempShipmentId, shipmentId,ShiroUtils.findUser());
		return new BaseData("0");
	}	
	
	@RequestMapping(value = "/legs/updStById", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updStById(@RequestParam(value = "ids", required = true)List<Long> ids,@RequestParam(value = "tempshipmentId", required = true)Long tempshipmentId) throws Exception {
		legsDao.updStById(ids,tempshipmentId,ShiroUtils.findUser());
		return new BaseData("0");
	}	  
	
	@RequestMapping(value = "/legs/id/{id}", method = RequestMethod.GET)
	@ResponseBody 
	public BaseData findById(@PathVariable Long id) {
		Legs legs = legsDao.findById(id);
		legs.setQuantity(Arith.round(legs.getQuantity(), 8));
		legs.setWeight(Arith.round(legs.getWeight(), 8));
		legs.setVolume(Arith.round(legs.getVolume(), 8));
		return new BaseData(legs, null);
	}
	
	@RequestMapping(value = "/legs/findByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData findByIds(@RequestParam(value = "ids", required = true) List<Long> ids) throws Exception {
		List<Legs> list=legsDao.findByIds(ids);
		return new BaseData(list,null);
	}	
	
	@RequestMapping(value = "/legs/del", method = RequestMethod.POST)
	@ResponseBody
	public BaseData del(@RequestBody  List<Legs> list,@RequestParam(value = "ids", required = true) List<Long> ids) {
		String str=legsService.delByIds1(list, ShiroUtils.findUser());
		if(str!=null){
			return new BaseData(str);
		}
		return new BaseData("0");
	}
		
	@RequestMapping(value = "/legs/findListBy", method = RequestMethod.POST)
	@ResponseBody
	public BaseData findListBy(@RequestBody BaseQueryForm baseQueryForm) {
		Page page=baseQueryForm.getPage();
		List<Legs> list = legsDao.findListBy(page,baseQueryForm.getFfMap(),ShiroUtils.findUser());
		BaseData baseData=new BaseData(list, page.getTotal());
		return baseData;
	}
	
	@RequestMapping(value = "/legs/findListBy1", method = RequestMethod.POST)
	@ResponseBody
	public BaseData findListBy1(@RequestBody BaseQueryForm baseQueryForm) {
		Page page=baseQueryForm.getPage();
		List<Legs> list = legsDao.findListBy1(page,baseQueryForm.getFfMap(),ShiroUtils.findUser());
		BaseData baseData=new BaseData(list, page.getTotal());
		return baseData;
	}
	
	@RequestMapping(value = "/legs/updTime", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updTime(@RequestParam(value = "ids", required = true)List<Long> ids, @RequestParam(value = "time", required = true)String time, @RequestParam(value = "status", required = true)String status, 
			@RequestParam(value = "shipmentIds", required = true) List<Long> shipmentIds,@RequestParam(value = "orderIds", required = true) List<Long> orderIds) throws Exception {
		List<Long> list = new ArrayList<Long>();
		legsDao.updTime(time, status, ids, list, ShiroUtils.findUser());
		for(int i=0;i<shipmentIds.size();i++){
			legsDao.zz("t_shipment", time, status, "shipmentId", shipmentIds.get(i));
		}
		for(int i=0;i<orderIds.size();i++){
			legsDao.zz("t_order_head", time, status, "orderId", orderIds.get(i));
		}
		return new BaseData("0");
	}	
	

    @RequestMapping(value = "/legs/export")
    public String exportFileTemplate(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			
			Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
			
			//Page page=baseQueryForm.getPage();
            String fileName = "分段订单数据.xlsx";
            
            List<Legs> list = legsDao.findListBy(null, params,ShiroUtils.findUser());

    		new ExportExcel("分段订单数据", Legs.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("msg", "分段订单数据导出失败。");
			logger.warn("importFileTemplate", e);
		}
		return "redirect:/msg";
    }
    private static final Logger logger = LoggerFactory.getLogger(LegsWeb.class);
}
